import copy

from rest_framework import serializers
from wxuser.serializers import UserSerializer
from wxuser.serializers import Users


from warehouse.models import JuqiGood, GoodLevel
from warehouse.serializers import JuqiGoodSerializer


from .models import (
    WuxianshangArgs,
    WuxianshangExtendBoss,
    WuxianshangExtendBossGood,
    WuxianshangGame,
    WuxianshangGameGood,
    WuxianshangRaffleRecord,
    TianjiangGoods,
)


class WuxianshangArgsSerializer(serializers.ModelSerializer):
    class Meta:
        model = WuxianshangArgs
        fields = "__all__"


class WuxianshangGameSerializer(serializers.ModelSerializer):
    class Meta:
        model = WuxianshangGame
        fields = "__all__"


def fill_WuxianshangGame(game):
    game_uuid = game["uuid"]
    game_goods = (
        WuxianshangGameGood.objects.filter(game_uuid=game_uuid)
        .filter(deleted=False)
        .order_by("sort")
        .order_by("id")
        .all()
    )
    detail_game_info = copy.deepcopy(game)
    goods = []

    for game_good in game_goods:
        good_uuid = game_good.good_uuid
        good = JuqiGood.objects.get(uuid=good_uuid)
        good_s = JuqiGoodSerializer(good)
        good_info = copy.deepcopy(good_s.data)
        good_info["weight"] = game_good.good_weight
        good_info["rarity"] = game_good.good_rarity
        goods.append(good_info)

    detail_game_info["goods"] = goods

    if game["extend_type_boss"]:
        extendBoss = WuxianshangExtendBoss.objects.filter(game_uuid=game_uuid).first()
        extendBoss_s = WuxianshangExtendBossSerializer(extendBoss)
        extend_boss = copy.deepcopy(extendBoss_s.data)
        extendBossGoods = WuxianshangExtendBossGood.objects.filter(game_uuid=game_uuid)
        extendBossGoods_s = WuxianshangExtendBossGoodSerializer(
            extendBossGoods, many=True
        )
        extend_boss["goods"] = copy.deepcopy(extendBossGoods_s.data)
        detail_game_info["extend_boss"] = extend_boss

    return detail_game_info


class WuxianshangRaffleRecordSerializer(serializers.ModelSerializer):
    class Meta:
        model = WuxianshangRaffleRecord
        fields = "__all__"


def fill_WuxianshangRaffleRecord(wrr: WuxianshangRaffleRecord):
    wrr_s = WuxianshangRaffleRecordSerializer(wrr)
    game = (
        WuxianshangGame.objects.filter(uuid=wrr.game_uuid).filter(deleted=False).first()
    )
    game_s = WuxianshangGameSerializer(game)

    good = JuqiGood.objects.filter(uuid=wrr.good_uuid).filter(deleted=False).first()
    good_s = JuqiGoodSerializer(good)

    good_info = copy.deepcopy(good_s.data)
    good_info["count"] = wrr.count

    # 隐私安全问题，返回尽可能少的个人数据
    user = Users.objects.filter(uuid=wrr.user_uuid).filter(deleted=False).first()

    res = {
        "game": game_s.data,
        "good": good_info,
        "user": {"nickname": user.nickname, "head_url": user.head_url},
        "record": wrr_s.data,
    }
    return res


class WuxianshangExtendBossSerializer(serializers.ModelSerializer):
    class Meta:
        model = WuxianshangExtendBoss
        fields = "__all__"


class WuxianshangExtendBossGoodSerializer(serializers.ModelSerializer):
    class Meta:
        model = WuxianshangExtendBossGood
        fields = "__all__"


class OneGoodInfoSer(serializers.ModelSerializer):
    detail = serializers.SerializerMethodField()

    class Meta:
        model = WuxianshangGameGood
        fields = (
            "uuid",
            "good_uuid",
            "detail",
            "good_weight",
            "level_uuid",
            "cp",
            "good_type",
            "score",
        )

    def get_detail(self, obj):
        return JuqiGoodSerializer(
            JuqiGood.objects.filter(uuid=obj.good_uuid).first(), many=False
        ).data


class RecoderSer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()
    good = serializers.SerializerMethodField()
    createdAt = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )

    class Meta:
        model = WuxianshangRaffleRecord
        fields = ("raffle_count", "createdAt", "user", "good")

    def get_user(self, obj):
        _u = Users.objects.filter(uuid=obj.user_uuid).first()
        return {"nickname": _u.nickname, "head_url": _u.head_url}

    def get_good(self, obj):
        _good = JuqiGood.objects.filter(uuid=obj.good_uuid).first()
        _level = (
            GoodLevel.objects.filter(game_type=3)
            .filter(title=_good.level_title)
            .first()
        )
        return {"good_name": _good.name, "level_url": _level.img_url}


class RecoderSerV2(serializers.Serializer):
    raffle_count = serializers.IntegerField(read_only=True)
    user_nickname = serializers.CharField(read_only=True)
    user_head_url = serializers.CharField(read_only=True)
    good_name = serializers.CharField(read_only=True)
    level_url = serializers.CharField(read_only=True)
    createdAt = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )


class TinajiangSerV2(serializers.Serializer):
    count = serializers.IntegerField(read_only=True)
    start_count = serializers.IntegerField(read_only=True)
    end_count = serializers.IntegerField(read_only=True)
    user_nickname = serializers.CharField(read_only=True)
    user_head_url = serializers.CharField(read_only=True)
    good_name = serializers.CharField(read_only=True)
    level_url = serializers.CharField(read_only=True)
    createdAt = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )


class FeiqiuSer(serializers.Serializer):
    start_count = serializers.IntegerField(read_only=True)
    end_count = serializers.IntegerField(read_only=True)
    user_nickname = serializers.CharField(read_only=True)
    user_head_url = serializers.CharField(read_only=True)
    good_name = serializers.CharField(read_only=True)
    level_url = serializers.CharField(read_only=True)
    createdAt = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )


class TinajiangSer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()
    good = serializers.SerializerMethodField()
    createdAt = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", required=False, read_only=True
    )

    class Meta:
        model = TianjiangGoods
        fields = "__all__"

    def get_user(self, obj):
        _u = Users.objects.filter(uuid=obj.user_uuid).first()
        return {"nickname": _u.nickname, "head_url": _u.head_url}

    def get_good(self, obj):
        _good = JuqiGood.objects.filter(uuid=obj.good_uuid).first()
        _level = (
            GoodLevel.objects.filter(game_type=3)
            .filter(title=_good.level_title)
            .first()
        )
        return {"good_name": _good.name, "level_url": _level.img_url}
